package com.example.demo.controller.client;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.jms.Topic;

@RestController
public class ActiveMQController {

    @Resource
    JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 测试MQ
     */
    @RequestMapping("testActiveMQ")
    public void testActiveMQ(){
        // 创建一个名字为ArticleVisitTopic的广播
        Topic topic = new ActiveMQTopic("ArticleVisitTopic");
        jmsMessagingTemplate.convertAndSend(topic, "这是一个广播消息！");
        // 创建一个名字为ArticleVisitQueue的队列
        Queue queue = new ActiveMQQueue("ArticleVisitQueue");
        // 向消息队列发送消息
        jmsMessagingTemplate.convertAndSend(queue, "这是一个队列消息！");
    }
}
